package com.erp.wallet.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.wallet.domain.ErpSellerStoreWalletExpend;
import com.erp.wallet.domain.bo.ErpSellerStoreWalletExpendBo;
import com.erp.wallet.domain.vo.ErpSellerStoreWalletExpendVo;
import com.erp.wallet.service.IErpSellerStoreWalletExpendService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户钱包拓展
 *
 * @author erp
 * @date 2023-08-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStoreWalletExpend")
public class ErpSellerStoreWalletExpendController extends BaseController {

    private final IErpSellerStoreWalletExpendService iErpSellerStoreWalletExpendService;

    /**
     * 获取登录用户的测评金额
     */
    @GetMapping("/getWalletExpend")
    public R<Object> getWalletExpend(@RequestParam("userId") Long userId) {
        if (userId == null) {
            return R.ok();
        }
        ErpSellerStoreWalletExpend walletExpend = iErpSellerStoreWalletExpendService.getWalletExpend(userId);
        return R.ok(walletExpend.getEvaluate());
    }

    /**
     * 修改测评金额
     */
    @GetMapping("/updateEvaluationAmount")
    public R<Object> updateEvaluationAmount(Integer flowType, Integer expenseType, Integer payType, Long sellerId, BigDecimal money, String remark) {

        String orderNo = "CP" + new Date().getTime()+"";
        return iErpSellerStoreWalletExpendService.updateAmount(flowType, expenseType, payType, sellerId, money, orderNo, remark);
    }

    /**
     * 查询用户钱包拓展列表
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStoreWalletExpendVo> list(ErpSellerStoreWalletExpendBo bo, PageQuery pageQuery) {
        return iErpSellerStoreWalletExpendService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户钱包拓展列表
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:export")
    @Log(title = "用户钱包拓展", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpSellerStoreWalletExpendBo bo, HttpServletResponse response) {
        List<ErpSellerStoreWalletExpendVo> list = iErpSellerStoreWalletExpendService.queryList(bo);
        ExcelUtil<ErpSellerStoreWalletExpendVo> excelUtil = new ExcelUtil<>(ErpSellerStoreWalletExpendVo.class);
        excelUtil.exportExcel(response,list,"用户钱包拓展");
    }

    /**
     * 获取用户钱包拓展详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:query")
    @GetMapping("/{id}")
    public R<ErpSellerStoreWalletExpendVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpSellerStoreWalletExpendService.queryById(id));
    }

    /**
     * 新增用户钱包拓展
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:add")
    @Log(title = "用户钱包拓展", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreWalletExpendBo bo) {
        return toAjax(iErpSellerStoreWalletExpendService.insertByBo(bo));
    }

    /**
     * 修改用户钱包拓展
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:edit")
    @Log(title = "用户钱包拓展", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreWalletExpendBo bo) {
        return toAjax(iErpSellerStoreWalletExpendService.updateByBo(bo));
    }

    /**
     * 删除用户钱包拓展
     *
     * @param ids 主键串
     */
    @SaCheckPermission("seller:sellerStoreWalletExpend:remove")
    @Log(title = "用户钱包拓展", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerStoreWalletExpendService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
